//
//  sound.m
//  Mockup
//
//  Created by Mr. Steptoe on 3/19/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Sound.h"
#import "OptionsVC.h"

@implementation Sound;
//@synthesize myMusic;

static AVAudioPlayer* myMusic;
int soundEnabled = 1;

+(void)initMusic {
	NSString *pathToMusicFile = [[NSBundle mainBundle] pathForResource: @"music1" ofType:@"mp3"];
	
	myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile] error:NULL];
	
	myMusic.delegate = self;
	myMusic.numberOfLoops = -1;
	myMusic.volume = 0.2;
	[myMusic play];
	//printf("music path initialized \n");
}

// sound effect calls
/*
 [Sound soundEffect:#]
 0	click
 1	poof
 8	beep
 9	fall
 10	bell
 11	bomb
 */

+(void) soundEffect:(int)soundNumber {
	NSString *effect;
	NSString *type;
	
	//printf("passed %d \n", soundNumber);
	
	//play sound 1, click.
	
	if (soundNumber == 0 && soundEnabled == 1) {
		printf("playing sound 0 \n");
		effect = @"click";
		type = @"mp3";
		
	}else if(soundNumber == 0 && soundEnabled == 0){
		return;
	}
	//play sound 2 poof, for matches
	else if (soundNumber == 1 && soundEnabled == 1) {
		
		effect = @"poof";
		type = @"wav";
		
		
	}
	else if(soundNumber == 1 && soundEnabled == 0){
		return;
	}
	else if (soundNumber == 8 && soundEnabled == 1) {
		
		effect = @"beep";
		type = @"wav";
		
		
	}
	else if(soundNumber == 8 && soundEnabled == 0){
		return;
	}
	else if (soundNumber == 9 && soundEnabled == 1) {
		
		effect = @"Fall";
		type = @"wav";
		
		
	}
	else if(soundNumber == 9 && soundEnabled == 0){
		return;
	}
	else if (soundNumber == 10 && soundEnabled == 1) {
		
		effect = @"Bell";
		type = @"wav";
		
		
	}
	else if(soundNumber == 10 && soundEnabled == 0){
		return;
	}
	else if (soundNumber == 11 && soundEnabled == 1) {
		
		effect = @"Bomb";
		type = @"wav";
		
		
	}
	else if(soundNumber == 11 && soundEnabled == 0){
		return;
	}
	
	else if (soundNumber == 2 && soundEnabled == 1){
		soundEnabled = 0;
		printf("sound off \n");
		return;
	}
	else if (soundNumber == 2 && soundEnabled == 0){
		soundEnabled = 0;
		return;
	}
	
	else if (soundNumber == 3 && soundEnabled == 0){
		soundEnabled = 1;	
		return;
	}
	else if (soundNumber == 3 && soundEnabled == 1){
		soundEnabled = 1;
		return;
	}	
	
	else if (soundNumber == 4 ){
		if([myMusic isPlaying]){
			[myMusic stop];
		}
		
		NSString *pathToMusicFile = [[NSBundle mainBundle] pathForResource: @"music1" ofType:@"mp3"];
		
		myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile] error:NULL];
		//[pathToMusicFile release];
		
		myMusic.delegate = self;
		myMusic.numberOfLoops = -1;
		myMusic.volume = 0.3;
		
		printf("set song 1 \n");
		
		//[myMusic play];
		return;
		
		
	}
	else if (soundNumber == 5 ){
		if([myMusic isPlaying]){
			[myMusic stop];
		}
		
		NSString *pathToMusicFile = [[NSBundle mainBundle] pathForResource: @"music2" ofType:@"wav"];
		
		myMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile] error:NULL];
		//[pathToMusicFile release];
		myMusic.delegate = self;
		myMusic.numberOfLoops = -1;
		myMusic.volume = 0.3;
		
		//printf("set song 2 \n");
		
		
		//[myMusic play];
		return;
		
	}
	
	else if (soundNumber == 6 ){
		
		if(![myMusic isPlaying]){
			[myMusic play];
			
		}
		
		return;
		
		
	}	
	else if (soundNumber == 7){
		
		[myMusic stop];
		
		printf("music turned off \n");
		return;
		
	}	
	//else if (soundNumber == 3) {
	
	//	effect = @"error";
	//	type = @"mp3";
	
	//	}
	
	NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"sound"];
	
	if ([value compare:@"ON"] == NSOrderedSame) {
		
		SystemSoundID soundID;
		
		NSString *path = [[NSBundle mainBundle] pathForResource:effect ofType:type];
		
		NSURL *url = [NSURL fileURLWithPath:path];
		
		AudioServicesCreateSystemSoundID ((CFURLRef)url, &soundID);
		
		AudioServicesPlaySystemSound(soundID);
		
	}
	
}

+(BOOL) checkIfSoundPlaying{
	if([myMusic isPlaying]){
		return TRUE;
	}else{
		return FALSE;
	}
}

+(BOOL) checkIfSoundOn{
	if(soundEnabled == 1){
		return TRUE;
	}else{
		return FALSE;
	}
	
}

- (void)dealloc {
	
	[super dealloc];
	
}



@end
